/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.emocbc.cbcgateway.smscbresponse;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.commons.io.IOUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 *
 * @author GiangLT
 */
public class SendMessageResponseParser {

    public SendMessageResponse domParse(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilder dBuilder = null;
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(inputStream);
            doc.getDocumentElement().normalize();

            Integer statusCode = 0;
            String requestResult = "";
            NodeList statusNodes = doc.getElementsByTagName("status");
            Node cbcMessageNode = doc.getElementsByTagName("cbcMessage").item(0);
            for (int i = 0; i < statusNodes.getLength(); i++) {
                Node statusNode = statusNodes.item(i);
                if (!statusNode.getParentNode().isSameNode(cbcMessageNode)) {
                    String textContent = statusNode.getTextContent();
                    try {
                        statusCode = Integer.valueOf(textContent);
                    } catch (NumberFormatException numberFormatException) {
                        requestResult = textContent;
                    }
                }
            }

            String messageId = doc.getElementsByTagName("messageId").item(0).getTextContent();
            String statusDescription = doc.getElementsByTagName("statusDescription").item(0).getTextContent();
            SendMessageResponse sendMessageResponse = new SendMessageResponse();
            sendMessageResponse.setMessageId(messageId);
            sendMessageResponse.setRequestResult(requestResult);
            sendMessageResponse.setStatus(statusCode);
            sendMessageResponse.setStatusDescription(statusDescription);

            return sendMessageResponse;
        } finally {
            if (dBuilder != null) {
                dBuilder.reset();
            }
            IOUtils.closeQuietly(inputStream);
        }
    }

    public static SendMessageResponse parse(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            SendMessageResponseHandler defaultHandler = new SendMessageResponseHandler();
            saxParser.parse(inputStream, defaultHandler);
            return defaultHandler.getSendMessageResponse();
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    }

    public static SendMessageResponse parse(String xml) throws ParserConfigurationException, SAXException, IOException {
        InputSource inputSource = new InputSource(new StringReader(xml));
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        SendMessageResponseHandler defaultHandler = new SendMessageResponseHandler();
        saxParser.parse(inputSource, defaultHandler);
        return defaultHandler.getSendMessageResponse();
    }
}
